package com.tarena.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.tarena.domain.User;
import com.tarena.util.HbnUtil;

public class UserDao {
	
	public UserDao(){
		super();
	}
	/**
	 * @Add a user
	 * @param user
	 */
	public void insertUser(User user){
		Session session  = HbnUtil.getSession();
		try{
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
		}catch(Exception e){
			session.getTransaction().rollback();
			e.printStackTrace();
		}
	}
	
	/**
	 * @Delete a user
	 * @param user
	 */
	public void deleteUser(User user){
		Session session  = HbnUtil.getSession();
		try{
			session.beginTransaction();
			session.delete(user);
			session.getTransaction().commit();
		}catch(Exception e){
			session.getTransaction().rollback();
			e.printStackTrace();
		}
	}
	
	/**
	 * @Get a user by id 
	 * @param user
	 * @return
	 */
	public boolean validate(User user) {
		Session session = HbnUtil.getSession();
		String hql = "select count(*) from t_user where email='"+user.getEmail()+"'";
		Query query = session.createSQLQuery(hql);
		List<User> list = query.list();
		System.out.println(list.size()+":UserDao ...58");
		System.out.println(user.getEmail());
		if(list.size() == 0){
			System.out.println("真的是等于0");
			return true;
		}
		return false;
	}

}
